package com.space.school.dao.impl;

import com.space.school.dao.LoginDao;
import com.space.school.domain.User;
import com.space.school.utils.SpaceJDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class LoginDaoImpl implements LoginDao {
    //    jdbcTemplate jdbc模板
    private JdbcTemplate jdbcTemplate=new JdbcTemplate(SpaceJDBCUtils.getDataSource());



    @Override
    public User login(String username, String password) {
//        登陆查询
        String sql = "select * from user where username=? and password=?";
//     sql行的映射器 把你查询的结果集放入<User>中
//        将sql 查询后的结果行映射到集合中
        List<User>list=jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class),username,password);
        if(list.size()>0){
            return list.get(0);
        }else {
            return null;
        }
    }

    @Override
    public User login_username(String username){
        String sql = "select * from user where username=?";
        List<User>list=jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class),username);
        if(list.size()>0){
            return list.get(0);
        }else {
            return null;
        }
    }

}
